﻿using System.Collections.Generic;
using com.guojin.mj.net;
using com.guojin.mj.net.message.login;
using UnityEngine;
using UnityEngine.UI;
using com.guojin.core.io.message;

internal enum ToggleType
{
    ChapterMax16Tog = 0,
    ChapterMax32Tog,
    MiddleJoinTog,
    BetweenOnePointJvGuoTog,
    CardCountsTog1,
    CardCountsTog2,
    ScoreRangeTog1,
    ScoreRangeTog2,
    ScoreRangeTog3,
    ScoreRangeTog4
}

internal enum TextType
{
    CardCountsTex1,
    CardCountsTex2,
    ScoreRangeTex1,
    ScoreRangeTex2,
    ScoreRangeTex3,
    ScoreRangeTex4,
}

public class CreateRoomPage : EventManager
{
    private static CreateRoomPage instance;
    public static CreateRoomPage GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public Toggle[] toggleArr;
    public Text[] textArr;
    public int CreateRoomType;
    public Button createRoombtn;
    public Button closeBtn;

    public override void InformationSetting()
    {
        CreateRoomType = 0;
        AddListenerToButton(createRoombtn, CreateRoomBtnClick);
        AddListenerToButton(closeBtn, CloseBtnClick);

        #region

        //public Button replaceCreatRoomBtn;
        // AddListenerToButton(replaceCreatRoomBtn, ReplaceCreateRoomBtnClick);

        #endregion
    }

    //显示创建房间返回的规则信息
    public void ShowCardCountsInText()
    {
        textArr[(int) TextType.CardCountsTex1].text = GetCardCountsStrFromCardCounts(CreateRoomController.GetInstance().cardCountsArr[0]);
        textArr[(int) TextType.CardCountsTex2].text = GetCardCountsStrFromCardCounts(CreateRoomController.GetInstance().cardCountsArr[1]);
    }

    private string GetCardCountsStrFromCardCounts(int i)
    {
        switch (i)
        {
            case 1:
                return "一副牌";
            case 2:
                return "二副牌";
            case 3:
                return "三副牌";
            case 4:
                return "四副牌";
            default:
                return "四副牌";
        }
    }

    public void ShowScoreRangeInText()
    {
        textArr[(int)TextType.ScoreRangeTex1].text = string.Format("{0}分", CreateRoomController.GetInstance().scoreRangeArr[0]);
        textArr[(int)TextType.ScoreRangeTex2].text = string.Format("{0}分", CreateRoomController.GetInstance().scoreRangeArr[1]);
        textArr[(int)TextType.ScoreRangeTex3].text = string.Format("{0}分", CreateRoomController.GetInstance().scoreRangeArr[2]);
        textArr[(int)TextType.ScoreRangeTex4].text = string.Format("{0}分", CreateRoomController.GetInstance().scoreRangeArr[3]);
    }

    //根据本地缓存设置toggle状态(当toggle的isOn值发生改变，自动触发事件事件，toggleGroup触发多个事件)
    public void SetChapterMax(string str)
    {
        if ((str == "16") || string.IsNullOrEmpty(str))
            toggleArr[(int)ToggleType.ChapterMax16Tog].isOn = true;
        else if (str == "32")
            toggleArr[(int)ToggleType.ChapterMax32Tog].isOn = true;
    }

    public void SetMiddleJoin(string str)
    {
        if ((str == "true") || string.IsNullOrEmpty(str))
            toggleArr[(int)ToggleType.MiddleJoinTog].isOn = true;
        else if (str == "false")
            toggleArr[(int)ToggleType.MiddleJoinTog].isOn = true;
    }

    public void SetBetweenOnePointJvGuo(string str)
    {
        if ((str == "true") || string.IsNullOrEmpty(str))
            toggleArr[(int)ToggleType.BetweenOnePointJvGuoTog].isOn = true;
        else if (str == "false")
            toggleArr[(int)ToggleType.BetweenOnePointJvGuoTog].isOn = true;
    }

    public void SetCardCounts(string str)           //这两个rule不记忆，规则改变无法记忆
    {
          toggleArr[(int)ToggleType.CardCountsTog1].isOn = true;
    }  
    public void SetScoreRange(string str)
    {
            toggleArr[(int)ToggleType.ScoreRangeTog1].isOn = true;
    }

    //创建房间传递的参数 和 toggle事件函数
    public void ChapterMax16TogClick()
    {
        if (toggleArr[(int)ToggleType.ChapterMax16Tog].isOn)
           CreateRoomController.GetInstance().chapterMax = "16";
    }

    public void ChapterMax32TogClick()
    {
        if (toggleArr[(int)ToggleType.ChapterMax32Tog].isOn)
            CreateRoomController.GetInstance().chapterMax = "32";
    }

    public void MiddleJoinTogClick()
    {
        if (toggleArr[(int)ToggleType.MiddleJoinTog].isOn)
            CreateRoomController.GetInstance().middleJoin = "true";
        else
            CreateRoomController.GetInstance().middleJoin = "false";
    }

    public void BetweenOnePointJvGuoTogClick()
    {
        if (toggleArr[(int)ToggleType.BetweenOnePointJvGuoTog].isOn)
            CreateRoomController.GetInstance().betweenOnePointJvGuo = "true";
        else
            CreateRoomController.GetInstance().betweenOnePointJvGuo = "false";
    }

    public void CardCountsTog1Click()
    {
        if (toggleArr[(int)ToggleType.CardCountsTog1].isOn)
            CreateRoomController.GetInstance().cardCounts = CreateRoomController.GetInstance().cardCountsArr[0].ToString();
    }

    public void CardCountsTog2Click()
    {
        if (toggleArr[(int)ToggleType.CardCountsTog2].isOn)
            CreateRoomController.GetInstance().cardCounts = CreateRoomController.GetInstance().cardCountsArr[1].ToString();
    }

    public void ScoreRangeTog1Click()
    {
        if (toggleArr[(int)ToggleType.ScoreRangeTog1].isOn)
            CreateRoomController.GetInstance().scoreRange = CreateRoomController.GetInstance().scoreRangeArr[0];
    }

    public void ScoreRangeTog2Click()
    {
        if (toggleArr[(int)ToggleType.ScoreRangeTog2].isOn)
            CreateRoomController.GetInstance().scoreRange = CreateRoomController.GetInstance().scoreRangeArr[1];
    }

    public void ScoreRangeTog3Click()
    {
        if (toggleArr[(int)ToggleType.ScoreRangeTog3].isOn)
            CreateRoomController.GetInstance().scoreRange = CreateRoomController.GetInstance().scoreRangeArr[2];
    }

    public void ScoreRangeTog4Click()
    {
        if (toggleArr[(int)ToggleType.ScoreRangeTog4].isOn)
            CreateRoomController.GetInstance().scoreRange = CreateRoomController.GetInstance().scoreRangeArr[3];
    }

    //创建房间
    private void CreateRoomBtnClick()
    {
        if (isCreatRoom)
        {
            //0代表普通房间  1代表代开房 2代表俱乐部房间
            MessageManager.GetInstance().SendMsg70(CreateRoomType, CreateRoomController.GetInstance().AppendRuleToCreateRoomMsg());
            isCreatRoom = false;
        }
    }

    private bool isCreatRoom = true;
    private float timer;
    private void Update()
    {
        if (!isCreatRoom)
        {
            timer += Time.deltaTime;
            if (timer > 2f)
            {
                timer = 0;
                isCreatRoom = true;
            }
        }
    }
}